package controllers

import (
	"github.com/gin-gonic/gin"
	"go.didapinche.com/foundation/apisix-dashboard/internal/app/models"
	"go.didapinche.com/foundation/apisix-dashboard/internal/app/services"
	"net/http"
)

type RouteController struct {
	service services.RouteService
}

func NewRouteController(service services.RouteService) *RouteController {
	return &RouteController{service: service}
}

func (ctl RouteController) Get(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	key := c.Param("id")
	u, err := ctl.service.Get(key, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "RouteService Get failed:%v", err)
	}
	c.JSON(http.StatusOK, u)
}

func (ctl RouteController) GetName(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	name := c.Query("name")
	u, err := ctl.service.GetName(name, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "RouteService GetName failed:%v", err)
	}
	c.JSON(http.StatusOK, u)
}

func (ctl RouteController) List(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	param := new(struct {
		Name     string `form:"key"`
		PageSize int    `form:"page_size"`
		PageNum  int    `form:"page_num"`
	})
	if err := c.ShouldBind(param); err != nil {
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}
	us, err := ctl.service.List(param.Name, env.(string), param.PageSize, param.PageNum)
	if err != nil {
		c.String(http.StatusBadRequest, "RouteService List failed:%v", err)
	}
	c.JSON(http.StatusOK, us)
}

func (ctl RouteController) Create(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	param := new(models.Route)
	if err := c.ShouldBind(param); err != nil {
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}
	err := ctl.service.Create(param, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "RouteService Create failed:%v", err)
	}
}
func (ctl RouteController) Update(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	param := new(models.Route)
	if err := c.ShouldBind(param); err != nil {
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}
	err := ctl.service.Update(param, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "UpstreamService Update failed:%v", err)
	}
}

func (ctl RouteController) Delete(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	key := c.Param("id")
	err := ctl.service.Delete(key, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "RouteService Delete failed:%v", err)
	}
	c.JSON(http.StatusOK, "")
}

func (ctl RouteController) Syn(c *gin.Context) {
	param := new(struct {
		Route *models.Route `json:"route"`
		Env   []string      `json:"env"`
	})
	if err := c.ShouldBind(param); err != nil {
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}
	for i := range param.Env {
		if param.Env[i] == "1" {
			param.Env[i] = "test"
		} else if param.Env[i] == "3" {
			param.Env[i] = "online"
		} else if param.Env[i] == "4" {
			param.Env[i] = "aliyun"
		} else {
			param.Env = append(param.Env[:1], param.Env[2:]...)
		}
	}
	err := ctl.service.Syn(param.Route, param.Env)
	if err != nil {
		c.String(http.StatusBadRequest, "RouteController Syn failed:%v", err)
	}
	c.JSON(http.StatusOK, "")
}
